package com.cl.cloud.common.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: liuzijian
 * @time: 2021-12-29 17:06
 */
@Slf4j
public class QuarterUtil {

    @Data
    public static class QuarterEntity {
        private Date beginTime;
        private Date endTime;
    }

    public static QuarterEntity getQuarterStartEndTime(int quarter) {
        QuarterEntity result = new QuarterEntity();
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        try {
            if (quarter == 1) {
                start.set(Calendar.MONTH, 0);
                start.set(Calendar.DATE, 1);
                end.set(Calendar.MONTH, 2);
                end.set(Calendar.DATE, 31);
            } else if (quarter == 2) {
                start.set(Calendar.MONTH, 3);
                start.set(Calendar.DATE, 1);
                end.set(Calendar.MONTH, 5);
                end.set(Calendar.DATE, 30);
            } else if (quarter == 3) {
                start.set(Calendar.MONTH, 6);
                start.set(Calendar.DATE, 1);
                end.set(Calendar.MONTH, 8);
                end.set(Calendar.DATE, 30);
            } else if (quarter == 4) {
                start.set(Calendar.MONTH, 9);
                start.set(Calendar.DATE, 1);
                end.set(Calendar.MONTH, 11);
                end.set(Calendar.DATE, 31);
            }
            end.add(Calendar.DATE, 1);

            result.setBeginTime(start.getTime());
            result.setEndTime(end.getTime());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    public static List<QuarterEntity> getQuarterStartEndTime(int beginQuarter, int endQuarter) {
        List<QuarterEntity> resultList = new ArrayList<>(endQuarter);
        for (int quarter = beginQuarter - 1; quarter <= endQuarter - 1; quarter++) {
            int tempQuarter = quarter + 1;
            QuarterEntity result = new QuarterEntity();
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            try {
                if (tempQuarter == 1) {
                    start.set(Calendar.MONTH, 0);
                    start.set(Calendar.DATE, 1);
                    end.set(Calendar.MONTH, 2);
                    end.set(Calendar.DATE, 31);
                } else if (tempQuarter == 2) {
                    start.set(Calendar.MONTH, 3);
                    start.set(Calendar.DATE, 1);
                    end.set(Calendar.MONTH, 5);
                    end.set(Calendar.DATE, 30);
                } else if (tempQuarter == 3) {
                    start.set(Calendar.MONTH, 6);
                    start.set(Calendar.DATE, 1);
                    end.set(Calendar.MONTH, 8);
                    end.set(Calendar.DATE, 30);
                } else if (tempQuarter == 4) {
                    start.set(Calendar.MONTH, 9);
                    start.set(Calendar.DATE, 1);
                    end.set(Calendar.MONTH, 11);
                    end.set(Calendar.DATE, 31);
                }
                //end.add(Calendar.DATE, 1);

                result.setBeginTime(start.getTime());
                result.setEndTime(end.getTime());
                resultList.add(result);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        return resultList;
    }


}
